package com.water.devtool;

import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.water.devtool.bean.FieldDefine;
import com.watermud.util.StringUtil;

public class BaseTableUtil {
	final static Logger log = LoggerFactory.getLogger(BaseTableUtil.class);

	public FieldDefine columnToField(Map m) {
		String columnName = (String) m.get("COLUMN_NAME");
		String comment = (String) m.get("COLUMN_COMMENT");
		String codeType = getCodeType(comment);
		log.debug("解析字段：{}", columnName);

		String fieldName = StringUtil.getJavaStyleName(columnName);
		FieldDefine fd = new FieldDefine();
		fd.setLongChar("");
		String javaTypeName = null;
		String jdbcName = null;
		String type = (String) m.get("DATA_TYPE");
		type = type.toUpperCase();
		if (type.startsWith("VARCHAR") || type.startsWith("CHAR")) {
			javaTypeName = "String";
			jdbcName = "String";

			int len = MapUtils.getIntValue(m, "CHARACTER_MAXIMUM_LENGTH");
			if (len > 64) {
				fd.setLongChar("1");
			}
		} else if (type.startsWith("NUMBER")) {
			int precision = MapUtils.getIntValue(m, "DATA_PRECISION");
			int dataScale = MapUtils.getIntValue(m, "DATA_SCALE");
			if (dataScale != 0) {
				javaTypeName = "Float";
				jdbcName = "Float";
			} else if (precision >= 10) {
				javaTypeName = "Long";
				jdbcName = "Long";
			} else {
				javaTypeName = "int";
				jdbcName = "Integer";
			}
		} else if (type.startsWith("INT") || type.startsWith("SMALLINT")) {
			javaTypeName = "Integer";
			jdbcName = "Int";
		} else if (type.startsWith("FLOAT")) {
			javaTypeName = "Float";
			jdbcName = "Float";
		} else if (type.startsWith("DATE") || type.startsWith("DATETIME") || type.startsWith("TIMESTAMP")) {
			javaTypeName = "Date";
			jdbcName = "Timestamp";
		} else if (type.startsWith("UUID")) {
			javaTypeName = "String";
			jdbcName = "String";
		} else {
			log.warn("数据库类型未解析：{}", type);
		}
		fd.setComment(comment);
		if (StringUtils.isBlank(comment)) {
			fd.setLabel(fieldName);
		} else {
			fd.setLabel(getLabel(comment));
		}
		fd.setJavaType(javaTypeName);
		fd.setJdbcType(jdbcName);
		fd.setDbName(columnName);
		fd.setDbType(type);
		fd.setName(fieldName);
		fd.setInsertAble("1");
		fd.setUpdateAble("1");
		fd.setCondition("0");
		fd.setVisiable("1");
		fd.setCodeType(codeType);
		return fd;
	}

	public static String getCodeType(String comment) {
		String codeType = null;
		int index = comment.indexOf("数据字典");
		if (index > -1) {
			int startIndex = index;
			int endIndex = index;
			for (int i = index + 4; i < comment.length(); i++) {
				char c = comment.charAt(i);
				if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_') {
					if (startIndex == index) {
						startIndex = i;
					}
				} else {
					if (startIndex > index) {
						endIndex = i;
						break;
					}
				}
			}
			if (endIndex == index) {
				endIndex = comment.length();
			}
			codeType = comment.substring(startIndex, endIndex);
		}
		return codeType;
	}

	public static String getLabel(String comment) {
		int length = comment.length();
		int index = comment.indexOf(" ");
		if (index > 9 || index == -1) {
			comment.indexOf(",");
		}
		if (index > 9 || index == -1) {
			comment.indexOf("，");
		}
		if (index > 9 || index == -1) {
			comment.indexOf(".");
		}
		if (index > 9 || index == -1) {
			comment.indexOf("。");
		}
		if (index > 9 || index == -1) {
			index = comment.indexOf(":");
		}
		if (index > 9 || index == -1) {
			index = comment.indexOf(";");
		}

		if (index > 0) {
			int len = index > 9 ? 9 : index;
			String label = comment.substring(0, len);
			return label;
		} else {
			int len = length > 9 ? 9 : length;
			String label = comment.substring(0, len);
			return label;
		}
	}

	public static void main(String[] args) {
		System.out.println(getCodeType("主键"));
	}
}
